<!DOCTYPE html>
<html lang="en">
<head>
    <link rel="stylesheet" href="../../plugins/layui/css/layui.css"/>
    <link rel="stylesheet" href="../../css/normalize.css"/>
    <link rel="stylesheet" href="../../css/fa.css"/>
    <link rel="stylesheet" href="../../css/icon.css"/>
    <link rel="stylesheet" href="../../css/ares_animate.min.css"/>
    <link rel="stylesheet" href="../../css/ares_hover_variant.min.css"/>
    <link rel="stylesheet" href="../../css/ares_reboot.css"/>
    <link rel="stylesheet" href="../../css/app.min.css"/>
    <link rel="stylesheet" href="../../css/iframe.css"/>
    <style>

    </style>

</head>
<body>
<form class="ares-p layui-form" lay-filter="form">
    <div class="ares-text-md">基本信息</div>
    <div class="layui-form-item">
        <label class="layui-form-label">患者姓名<i class="ares-text-red">*</i></label>
        <div class="layui-input-block">
            <input name="name" placeholder="请输入姓名" class="layui-input" lay-verify="required">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">患者编号<i class="ares-text-red">*</i></label>
        <div class="layui-input-block">
            <input name="identity" placeholder="请输入身份证号" class="layui-input" lay-verify="required|identity">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">出生日期</label>
        <div class="layui-input-block">
            <input name="birthday" id="birthday" placeholder="请选择出生日期" class="layui-input" readonly>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">病患类型<i class="ares-text-red">*</i></label>
        <div id="patient-classify-wrap" class="layui-input-block"></div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">性别</label>
        <div id="sex-wrap" class="layui-input-block"></div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">医保类型</label>
        <div id="insurance-type-wrap" class="layui-input-block"></div>
    </div>


    <div class="ares-text-md">会诊信息</div>
    <div class="layui-form-item">
        <label class="layui-form-label">床旁治疗</label>
        <div class="layui-input-block">
            <input type="radio" name="classify" value="1" title="是" checked>
            <input type="radio" name="classify" value="0" title="否">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">病区/病床<i class="ares-text-red">*</i></label>
        <div class="layui-input-block">
            <div class="layui-input-inline">
                <input name="wardArea" placeholder="请输入病区" class="layui-input" lay-verify="required">
            </div>
            <div class="layui-input-inline">
                <input name="bedNumber" placeholder="请输入病床" class="layui-input" lay-verify="required">
            </div>
        </div>

    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">治疗组<i class="ares-text-red">*</i></label>
        <div id="group-wrap" class="layui-input-block"></div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">会诊科室<i class="ares-text-red">*</i></label>
        <div id="division-wrap" class="layui-input-block"></div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">主诉<i class="ares-text-red">*</i></label>
        <div class="layui-input-block">
            <textarea name="chief" placeholder="请输入主诉" class="layui-textarea" lay-verify="required"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="submit">提交表单</button>
        </div>
    </div>
</form>


<script type="text/javascript" src="../../plugins/layui/layui.js"></script>
<script type="text/javascript" src="../../aresExtends/config/layuiConfig.js"></script>
<script>
    layui.use(['laydate', 'form', 'consultationServices', 'groupServices', 'utils', 'widget'], function (laydate, form, consultationServices, groupServices, utils, widget) {

        init();

        /**
         * @author: ares
         * @date: 2021/3/25 10:00
         * @description: 提交
         */
        form.on('submit(submit)', function (data) {
            var fields = data.field,
                $this = $(data.elem),
                data = {
                    emr: {
                        insurance_type: fields.insuranceType,
                        patient_classify: fields.patientClassify,
                        patient: {
                            name: fields.name,
                            real_name: fields.name,
                            sex: fields.sex,
                            identity_card: fields.identity,
                            birthday: fields.birthday ? new Date(fields.birthday).getTime() : ''
                        }
                    },
                    wardArea: fields.wardArea,
                    bedNumber: fields.bedNumber,
                    division_id: fields.division,
                    treatmentGroupIdList: getCheckGroups(),
                    classify: fields.classify,
                    main_narrate: fields.chief,
                };
            $this.prop('disabled', true).addClass('layui-btn-disabled')
            consultationServices.createConsultationService(data).then(function (res) {
                utils.msgSuccess(res.message)
                $this.prop('disabled', false).removeClass('layui-btn-disabled')
                utils.layerCloseAll('iframe');
                utils.ARES.event.emit('updateConsultationList');
            }).catch(function (err) {
                $this.prop('disabled', false).removeClass('layui-btn-disabled')
            })
            return false;
        });

        /**
         * @author: ares
         * @date: 2021/3/25 10:10
         * @description: 获取已选择的治疗组
         */
        function getCheckGroups() {
            var formFields = form.val('form'),
                groupIds = [];
            for (key in formFields) {
                if (key.indexOf('group_') == 0) groupIds.push(formFields[key]);
            }
            return groupIds;
        }

        /**
         * @author: ares
         * @date: 2021/3/16 21:12
         * @description: 获取会诊科室
         */
        function getConsultationDevision() {
            consultationServices.getConsultationDevisionService().then(function (res) {
                $('#division-wrap').empty().append(utils.createSelect('division', res.data.list, res.data.list[0].id, {
                    name: 'name',
                    value: 'id',
                    verify: {'lay-verify': 'required'}
                }));
                form.render('select');
            });
        }

        /**
         * @author: ares
         * @date: 2021/3/25 16:33
         * @description: 生成性别选项
         */
        function renderPatientSexRadio() {
            $('#sex-wrap').empty().append(widget.createSexRadio())
        }

        /**
         * @author: ares
         * @date: 2021/3/25 16:38
         * @description: 生成医保类型
         */
        function renderInsuranceTypeRadio() {
            $('#insurance-type-wrap').empty().append(widget.createInsuranceTypeRadio())
        }

        /**
         * @author: ares
         * @date: 2021/3/25 21:51
         * @description: 生成治疗组
         */
        function renderGroupCheckbox() {
            groupServices.getListForHospitalService().then(function (res) {
                var list = res.data || [];
                $('#group-wrap').empty().append(utils.createCheckbox('group', list, [], {
                    name: 'abbr',
                    value: 'id',
                    verify: {'lay-verify': 'requiredCheckBox', 'lay-verify-label': '治疗组'}
                }))
                form.render('checkbox')
            })
        }

        /**
         * @author: ares
         * @date: 2021/3/25 15:26
         * @description: 渲染病患类型下拉列表
         */
        function renderPatientClassifySelect() {
            $('#patient-classify-wrap').empty().append(widget.createPatientClassifySelect('patientClassify', {
                verify: {'lay-verify': 'required'}
            }));
            form.render('select');
        }

        /**
         * @author: ares
         * @date: 2021/3/16 21:12
         * @description: 初始化
         */
        function init() {
            laydate.render({
                elem: '#birthday',
                showBottom: false,
            });
            getConsultationDevision();
            renderPatientSexRadio();
            renderInsuranceTypeRadio();
            renderPatientClassifySelect();
            renderGroupCheckbox();
            form.render();
        }
    })
</script>
</body>
</html>